package cn.wen.java.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import cn.wen.java.io.exceptions.SerializationException;
import cn.wen.java.utils.Assert;

/**
 * Title: 序列化相关
 * <p>
 * Description: Java 序列化相关的辅助类
 * <p>
 * Copyright: Copyright (c) 2010-2012
 * 
 * @author <a href="mailto:ct_wen@qq.com">ctwen</a>, 温承瑭
 * @version Revision: 1.0 Date: 2010/12/6 description：初完成。
 *          <P>
 *          Revision: 1.5 Date: 2011/2/14 description：实现序列化对象。
 * 
 */
public class Serializer {

    // Serialize
    // -----------------------------------------------------------------------
    /**
     * <p>
     * Serializes an <code>Object</code> to the specified stream.
     * </p>
     * 
     * <p>
     * The stream will be closed once the object is written. This avoids the need for a finally clause, and maybe also exception
     * handling, in the application code.
     * </p>
     * 
     * <p>
     * The stream passed in is not buffered internally within this method. This is the responsibility of your application if
     * desired.
     * </p>
     * 
     * @param obj the object to serialize to bytes, may be null
     * @param outputStream the stream to write to, must not be null
     * @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
     * @throws SerializationException (runtime) if the serialization fails
     */
    public static void serialize(Serializable obj, OutputStream outputStream) {
	Assert.isNotNull(outputStream, "The OutputStream must not be null");
	ObjectOutputStream out = null;
	try {
	    // stream closed in the finally
	    out = new ObjectOutputStream(outputStream);
	    out.writeObject(obj);
	}
	catch (IOException ex) {
	    throw new SerializationException(ex);
	}
	finally {
	    HelperOfIO.closes(out);
	}
    }

    /**
     * <p>
     * Serializes an <code>Object</code> to a byte array for storage/serialization.
     * </p>
     * 
     * @param obj the object to serialize to bytes
     * @return a byte[] with the converted Serializable
     * @throws SerializationException (runtime) if the serialization fails
     */
    public static byte[] serialize(Serializable obj) {
	ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
	serialize(obj, baos);
	return baos.toByteArray();
    }

    // Deserialize
    // -----------------------------------------------------------------------
    /**
     * <p>
     * Deserializes an <code>Object</code> from the specified stream.
     * </p>
     * 
     * <p>
     * The stream will be closed once the object is written. This avoids the need for a finally clause, and maybe also exception
     * handling, in the application code.
     * </p>
     * 
     * <p>
     * The stream passed in is not buffered internally within this method. This is the responsibility of your application if
     * desired.
     * </p>
     * 
     * @param inputStream the serialized object input stream, must not be null
     * @return the deserialized object
     * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
     * @throws SerializationException (runtime) if the serialization fails
     */
    public static Object deserialize(InputStream inputStream) {
	Assert.isNotNull(inputStream, "The InputStream must not be null");
	ObjectInputStream in = null;
	try {
	    // stream closed in the finally
	    in = new ObjectInputStream(inputStream);
	    return in.readObject();
	}
	catch (ClassNotFoundException ex) {
	    throw new SerializationException(ex);
	}
	catch (IOException ex) {
	    throw new SerializationException(ex);
	}
	finally {
	    HelperOfIO.closes(in);
	}
    }

    /**
     * <p>
     * Deserializes a single <code>Object</code> from an array of bytes.
     * </p>
     * 
     * @param objectData the serialized object, must not be null
     * @return the deserialized object
     * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code>
     * @throws SerializationException (runtime) if the serialization fails
     */
    public static Object deserialize(byte[] objectData) {
	Assert.isNotNull(objectData, "The byte[] must not be null");
	ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
	return deserialize(bais);
    }

}
